/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.propertytree.propertyhandler;

import java.io.Serializable;
import java.util.Map;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import hu.tetete.hibiscus.R;
import hu.tetete.hibiscus.model.OneSelectProperty;
import hu.tetete.hibiscus.propertyvalue.PropertyValue;
import hu.tetete.hibiscus.propertyvalue.SelectPropertyValue;

/**
 * TODO javadoc.
 */
public class OneSelectHandler implements PropertyHandler<OneSelectProperty, SelectPropertyValue> {

    /** @see Serializable */
    private static final long serialVersionUID = 1L;

    /**
     * The context.
     */
    private final Context                  context;
    /**
     * The selected values for all properties.
     */
    private final Map<Long, PropertyValue> values;
    /**
     * The property.
     */
    private final OneSelectProperty        property;

    /**
     * @param context The context
     * @param values The selected values for all properties
     * @param property The property
     */
    public OneSelectHandler(final Context context,
            final Map<Long, PropertyValue> values,
            final OneSelectProperty property) {
        this.context  = context;
        this.values   = values;
        this.property = property;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getView()
     */
    @Override
    public View getView() {

        final LayoutInflater        inflater      = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View                  view          = inflater.inflate(R.layout.onselect_property, null);
        final Spinner               spinner       = (Spinner)view.findViewById(R.id.oneSelectSelector);

        final SelectPropertyValue[] options       = new SelectPropertyValue[this.property.getOptions().size() + 1];
        final SelectPropertyValue   selectedValue = getValue();

        int index = 0;
        int selectedPosition = 0;

        options[index++] = new SelectPropertyValue(null, "Not selected");

        for (final SelectPropertyValue option : this.property.getOptions()) {
            if (selectedValue == option) {
                selectedPosition = index;
            }
            options[index++] = option;
        }

        @SuppressWarnings({ "rawtypes", "unchecked" })
        final ArrayAdapter adapter = new ArrayAdapter(this.context, android.R.layout.simple_spinner_item, options);

        spinner.setAdapter(adapter);
        spinner.setSelection(selectedPosition);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            /**
             * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, android.view.View, int, long)
             */
            @SuppressWarnings("synthetic-access")
            @Override
            public void onItemSelected(final AdapterView<?> parent, final View clickedView,
                    final int position, final long id) {
                Toast.makeText(OneSelectHandler.this.context, "You selected: " + position + " id: " + id, Toast.LENGTH_LONG).show();
                final SelectPropertyValue value = options[position];
                if (value.getId() == null) {
                    setValue(null);
                } else {
                    setValue(value);
                }
            }

            @SuppressWarnings("synthetic-access")
            @Override
            public void onNothingSelected(final AdapterView<?> adapterView) { //TODO handle properly, what is this?
                Toast.makeText(OneSelectHandler.this.context, "You haven't selected anything.", Toast.LENGTH_LONG).show();

            }
        });
        return view;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getInfoView()
     */
    @Override
    public View getInfoView() {
        final TextView textView = new TextView(this.context);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        final SelectPropertyValue value = getValue();
        final String text = value == null ? "Not selected" : value.getName();
        textView.setText(text);
        textView.setTextSize(5);
        textView.setMaxLines(2);
        textView.setMaxWidth(30);
        return textView;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getProperty()
     */
    @Override
    public OneSelectProperty getProperty() {
        return this.property;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#setValue(hu.tetete.hibiscus.propertyvalue.PropertyValue)
     */
    @Override
    public void setValue(final SelectPropertyValue value) {
        if (value == null) {
            this.values.remove(this.property.getId());
        }
        this.values.put(this.property.getId(), value);
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getValue()
     */
    @Override
    public SelectPropertyValue getValue() {
        return (SelectPropertyValue)this.values.get(this.property.getId());
    }

}
